﻿using ArivisAccounts.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Security;
using WebMatrix.WebData;

namespace ArivisAccounts
{
    public class AuthConfig
    {
        public static void RegisterAuth()
        {
            Database.SetInitializer<UserProfilesDbContext>(null);
            using (var context = new UserProfilesDbContext())
            {
                if (!context.Database.Exists())
                {
                    context.Database.CreateIfNotExists();
                }
            }

            WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

            // make sure there is at least one 'Admin'-User
            if (!Roles.RoleExists("Admin"))
            {
                Roles.CreateRole("Admin");
            }

            // make sure there is at least one 'Admin'-User
            if (Roles.GetUsersInRole("Admin").Length == 0)
            {
                // Create 'Admin'-User
                if (!WebSecurity.UserExists("Admin"))
                {
                    WebSecurity.CreateUserAndAccount("Admin", "Admin", requireConfirmationToken: false);
                }
                // Assign 'Admin'-User to 'Admin'-Group
                Roles.AddUserToRole("Admin", "Admin");
            }
        }
    }
}